package com.example.demo.dismanage.service.impl;

import com.example.demo.dismanage.mapper.DisMapper;
import com.example.demo.dismanage.service.DisService;
import com.example.demo.loginmanage.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class DisServiceImpl implements DisService {

    @Autowired
    private DisMapper disMapper;

    @Autowired
    private LoginService loginService;


    @Override
    public Map<String, Object> yueche(Map<String, Object> param) {
        List<Map<String,Object>> paiList = new ArrayList<Map<String, Object>>();
        paiList = disMapper.queryAllPaiMan(param);
        int i = disMapper.yuecheMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("Status","1");
        }else{
            resultMap.put("Status","0");
        }
        resultMap.put("paiList",paiList);
        return resultMap;
    }

    /**
     * @Description 查询当前行驶公里数
     * @Author zhangxue
     * @Date 17:34 2018/7/11
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> queryNowFankilo(Map<String, Object> param) {
        Map<String,Object> CarMap = disMapper.queryNowFankilo(param);
        String FanStartKilo = CarMap.get("Mileage")!=null?CarMap.get("Mileage").toString():"0";
        String FanEndKilo = param.get("FanEndKilo")!=null?param.get("FanEndKilo").toString():"0";
        Integer FanKilo = Integer.valueOf(FanEndKilo)-Integer.valueOf(FanStartKilo);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("FanKilo",FanKilo);
        return resultMap;

    }

    /**
     * @Description 根据条件查询所有调度信息
     * @Author zhangxue
     * @Date 17:34 2018/7/11
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> queryAllDoc(Map<String, Object> param) {
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        resultList = disMapper.queryAllDocMapper(param);
        for(int i=0;i<resultList.size();i++){
            String FanDateStr = resultList.get(i).get("FanDate")!=null?resultList.get(i).get("FanDate").toString():null;
            String YueStartDateStr = resultList.get(i).get("YueStartDate")!=null?resultList.get(i).get("YueStartDate").toString():null;
            String YueEndDateStr = resultList.get(i).get("YueEndDate")!=null?resultList.get(i).get("YueEndDate").toString():null;
            try {
                if(FanDateStr!=null){
                    Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(FanDateStr);
                    FanDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date1);

                }
                Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(YueStartDateStr);
                YueStartDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date2);

                Date date3 = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(YueEndDateStr);
                YueEndDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date3);
                resultList.get(i).put("FanDate",FanDateStr);
                resultList.get(i).put("YueStartDate",YueStartDateStr);
                resultList.get(i).put("YueEndDate",YueEndDateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Map<String,String> countMap = disMapper.queryAllDocCountMapper(param);
        Object totalSize = countMap.get("total");
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("data",resultList);
        resultMap.put("totalnum",totalSize);
        return resultMap;

    }

    /**
     * @Description 根据条件查询所有约车调度信息
     * @Author zhangxue
     * @Date 17:34 2018/7/11
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> queryAllDis(Map<String, Object> param) {
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        resultList = disMapper.queryAllDisMapper(param);
        for(int i=0;i<resultList.size();i++){
            String YueStartDateStr = resultList.get(i).get("YueStartDate")!=null?resultList.get(i).get("YueStartDate").toString():null;
            String YueEndDateStr = resultList.get(i).get("YueEndDate")!=null?resultList.get(i).get("YueEndDate").toString():null;
            try {
                Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(YueStartDateStr);
                YueStartDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date2);

                Date date3 = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(YueEndDateStr);
                YueEndDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date3);
                resultList.get(i).put("YueStartDate",YueStartDateStr);
                resultList.get(i).put("YueEndDate",YueEndDateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        Map<String,String> countMap = disMapper.queryAllDisCountMapper(param);
        Object totalSize = countMap.get("total");
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("data",resultList);
        resultMap.put("totalnum",totalSize);
        return resultMap;

    }

    /**
     * @Description 根据条件查询所有约车调度信息
     * @Author zhangxue
     * @Date 17:34 2018/7/11
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> queryAllYueDis(Map<String, Object> param) {
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        resultList = disMapper.queryAllYueDisMapper(param);
        Map<String,String> countMap = disMapper.queryAllYueDisCountMapper(param);
        Object totalSize = countMap.get("total");
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("data",resultList);
        resultMap.put("totalnum",totalSize);
        return resultMap;

    }

    /**
     * @Description 删除调度信息
     * @Author zhangxue
     * @Date 17:34 2018/7/11
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> delDis(Map<String, Object> param) {
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap = disMapper.queryDisMapper(param);
        Map<String,Object> result = new HashMap<String, Object>();
        if(resultMap==null){
            result.put("Status","-1");
            return result;
        }
        String Status = resultMap.get("Status")!=null?resultMap.get("Status").toString():null;
        if("1".equals(Status)){
            int i = disMapper.delDis(param);
            if(i==1){
                result.put("Status","1");
                return result;
            }else{
                result.put("Status","0");
                return result;
            }
        }else if("2".equals(Status)){
            result.put("Status","2");
            return result;
        }else if("3".equals(Status)){
            result.put("Status","3");
            return result;
        }else{
            return null;
        }
    }

    /**
     * @Description 派车
     * @Author zhangxue
     * @Date 17:34 2018/7/11
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> SavePaiInfo(Map<String, Object> param) {
        int i = disMapper.SavePaiInfoMapper(param);
        int j = disMapper.updateManStatusMapper(param);
//        String IsOrNotPin = param.get("PailsOrNotPin")!=null?param.get("PailsOrNotPin").toString():null;
        int k = disMapper.updateCarStatusMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(k==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;

    }

    /**
     * @Description 撤销派车为已约车状态
     * @Author zhangxue
     * @Date 17:34 2018/7/11
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> delPaiInfo(Map<String, Object> param) {
        List<Map<String,Object>> paramList = disMapper.queryAllDocByCarNo(param);
        param.put("paramList",paramList);
        int i = disMapper.delPaiInfoMapper(param);
        int j = disMapper.updateManStatusMapperToYue(param);
        int k = disMapper.updateCarStatusMapperToYue(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(k==1){
            resultMap.put("status","1");
            resultMap.put("paramList",paramList);
        }else{
            resultMap.put("status","0");
        }
        return resultMap;

    }

    /**
     * @Description 撤销约车
     * @Author zhangxue
     * @Date 17:34 2018/7/11
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> CheYue(Map<String, Object> param) {
        int i = disMapper.CheYue(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;

    }

    /**
         * @Description 查询所有公司车辆
         * @Author zhangxue
         * @Date 8:50 2018/7/31
         * @Param
         * @retrun
         **/
    @Override
    public List<Map<String, Object>> queryAllCar(Map<String, Object> param) {
        List<Map<String,Object>> resultList = disMapper.queryAllCarMapper(param);
        return resultList;

    }

    /**
     * @Description 查询所有司机店面
     * @Author zhangxue
     * @Date 8:50 2018/7/31
     * @Param
     * @retrun
     **/
    @Override
    public List<Map<String, Object>> queryAllCarManStore(Map<String, Object> param) {
        List<Map<String,Object>> resultList = disMapper.queryAllCarManStoreMapper(param);
        return resultList;

    }

    /**
         * @Description 派车查询部门下拉列表
         * @Author zhangxue
         * @Date 10:22 2018/7/31
         * @Param
         * @retrun
         **/
    @Override
    public List<Map<String, Object>> queryBmByStore(Map<String, Object> param) {
        List<Map<String,Object>> resultList = disMapper.queryBmByStoreMapper(param);
        return resultList;

    }

    /**
     * @Description 派车查询司机下拉列表
     * @Author zhangxue
     * @Date 10:22 2018/7/31
     * @Param
     * @retrun
     **/
    @Override
    public List<Map<String, Object>> queryUserByStoreAndBm(Map<String, Object> param) {
        List<Map<String,Object>> resultList = disMapper.queryUserByStoreAndBm(param);
        return resultList;

    }

    /**
     * @Description 查询司机任务（手机端接口）
     * @Author zhangxue
     * @Date 10:22 2018/7/31
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> querySjTaskBySjNo(Map<String, Object> param) {
        List<Map<String,Object>> resultList = disMapper.querySjTaskBySjNo(param);

        for(int i=0;i<resultList.size();i++){
            String YuePhoneNo = "";
            String YueNo = resultList.get(i).get("YueManNo")!=null?resultList.get(i).get("YueManNo").toString():null;
            Map<String,Object> tempMap = new HashMap<String, Object>();
            tempMap.put("UserNo",YueNo);
            Map<String,Object> PhoneMap = loginService.queryPhoneByUserNo(tempMap);
            YuePhoneNo = PhoneMap.get("PhoneNo")!=null?PhoneMap.get("PhoneNo").toString():"";
            resultList.get(i).put("YuePhoneNo",YuePhoneNo);
        }

        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("data",resultList);
        return resultMap;

    }
}
